﻿using System;
using System.Windows;
using System.Windows.Controls;

namespace WPFTestSolution.Pages.Controls.ItemsControls.Selectors.MultiSelectors {
    
    /**
     * https://www.bilibili.com/video/BV1bV411A7Eq?p=55
     * 055-DataGrid数据表格控件(: MultiSelector)
     * https://www.wpfsoft.com/2023/09/06/1827.html
     *
     * TODO: 怎么在xaml中的DataGrid直接添加并显示数据??
     */
    public partial class _55_DataGrid_Page : Page {
        private class Person2 {
            public string Name { get; set; }
            public int Age { get; set; }
            public DateTime HireDate { get; set; }
            public bool IsActive { get; set; }
            public string Address { get; set; }
        }
        
        
        public _55_DataGrid_Page() {
            InitializeComponent();
            this.Title = "055-DataGrid数据表格控件(: MultiSelector)";

            this.Button_Add.Click += (sender, args) => {
                int count = this.DataGrid.Items.Count;
                this.DataGrid.Items.Add(new Person2 { Name = $"新增{count - 3}", Age = 24 + count - 2, HireDate = new DateTime(), IsActive = count % 2 == 0,  Address = $"新增地址{count - 3}" });
            };
            
            this.Button_Remove.Click += (sender, args) => {
                int count = this.DataGrid.Items.Count;
                this.DataGrid.Items.RemoveAt(count - 1);
            };
            
            this.Button_Edit.Click += (sender, args) => {
                int count = this.DataGrid.Items.Count;
                object obj = this.DataGrid.Items.GetItemAt(count - 1);
                if (!(obj is Person2 person)) return;
                person.Address = $"编辑地址{count - 4}";
                this.DataGrid.Items.Refresh();
            };
            
            this.DataGrid.Items.Add(new Person2 { Name = "张三", Age = 22, HireDate = new DateTime(2020, 2, 3), IsActive = true,  Address = "广东省廉江市车板镇大坝村" });
            this.DataGrid.Items.Add(new Person2 { Name = "李四", Age = 23, HireDate = new DateTime(2021, 4, 5), IsActive = false, Address = "江西省景德镇市市辖区" });
            this.DataGrid.Items.Add(new Person2 { Name = "王五", Age = 24, HireDate = new DateTime(2022, 6, 7), IsActive = true, Address = "上海市虹口区" });
            
            /**
             * 需要注意的细节是：我们将DataGrid的IsReadOnly="True"，这是因为我们直接将数据元素一条一条的加入到DataGrid的Items属性中，而Items属性本身是一个只读属性，不支持写入。<LineBreak />
             * 这样的话，当鼠标“双击”时会报错。错误提示为：System.InvalidOperationException: 此视图不允许“EditItem” <LineBreak />
             * 如何解决这个问题呢？这就要用到ItemsControl基类中的ItemsSource数据源属性。<LineBreak />
             * 我们需要采用DataGrid另外一种赋值方式——数据源赋值。即把一个集合绑定到该属性上，这样在前端就可以编辑数据源，从而不会引发报错。
             */
            // list.Add(...);
            // this.DataGrid.ItemsSource = list;
        }

        private void ViewButton_Click(object sender, RoutedEventArgs e) {
            Console.WriteLine($"sender={sender}, e={e}");
            // if (!(sender is DataGrid dataGrid)) return;
            //
            if (!(this.DataGrid.SelectedItem is Person2 person)) return;

 
            this._TextBlockContent.Text = $"Name={person.Name}, Age={person.Age}, HireDate={person.HireDate}, IsActive={person.IsActive}, Address={person.Address}";
        }
    }
}